package com.wooophone.mpyeah.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.wooophone.common.util.YamlUtil;
import com.wooophone.mpyeah.biz.BrandCategoryBiz;
import com.wooophone.mpyeah.biz.exception.CannotDeleteException;
import com.wooophone.mpyeah.dao.BrandCategoryDao;
import com.wooophone.mpyeah.entity.Brand;
import com.wooophone.mpyeah.entity.BrandCategory;

public class BrandCategoryBizImpl implements BrandCategoryBiz {

	@Autowired
	BrandCategoryDao brandCategoryDao;

	public void setBrandCategoryDao(BrandCategoryDao brandCategoryDao) {
		this.brandCategoryDao = brandCategoryDao;
	}

	@Override
	public BrandCategory save(BrandCategory obj) {
		return brandCategoryDao.save(obj);
	}

	@Override
	public int update(BrandCategory obj) {
		return brandCategoryDao.update(obj);
	}

	@Override
	public int delete(String id) throws CannotDeleteException {
		BrandCategory brandCategory = brandCategoryDao.get(id);
		List<BrandCategory> children = brandCategory.getChildren();
		List<Brand> brands = brandCategory.getBrands();
		if (children != null && children.size() > 0) {
			throw new CannotDeleteException("该分类下存在子级分类，不能进行删除，请将其全部删除后再试！");
		}
		if (brands != null && brands.size() > 0) {
			throw new CannotDeleteException("该分类下存在品牌信息，不能进行删除，请将其全部删除后再试！");
		}
		return brandCategoryDao.delete(id);
	}

	@Override
	public BrandCategory get(String id) {
		return brandCategoryDao.get(id);
	}

	@Override
	public List<BrandCategory> list() {
		return brandCategoryDao.list();
	}

	@Override
	public List<BrandCategory> list(int offset, int limit) {
		return brandCategoryDao.list(offset, limit);
	}

	@Override
	public int count() {
		return brandCategoryDao.count();
	}

	@Override
	public List<BrandCategory> find(Map<String, Object> parameter) {
		return brandCategoryDao.find(parameter);
	}

	@Override
	public List<BrandCategory> find(Map<String, Object> parameter, int offset,
			int limit) {
		return brandCategoryDao.find(parameter, offset, limit);
	}

	@Override
	public int count(Map<String, Object> parameter) {
		return brandCategoryDao.count(parameter);
	}

	@Override
	public int count(String keyword) {
		return brandCategoryDao.count(keyword);
	}

	@Override
	public List<BrandCategory> search(String keyword, int offset, int limit) {
		return brandCategoryDao.search(keyword, offset, limit);
	}

	@Override
	public List<BrandCategory> findTopCategory() {
		return brandCategoryDao.findTopCategory();
	}

	@Override
	public List<BrandCategory> findByParentId(String parentId) {
		return brandCategoryDao.selectByParentId(parentId);
	}

	@Override
	public List<Object> findRecommendGradeFields(String... cids) {
		List<Object> list = new ArrayList<Object>();
		List<Object> fields;
		for (String cid : cids) {
			fields = brandCategoryDao.get(cid).getGradeFieldList();
			if (fields != null)
				list.addAll(fields);
		}
		
		/*去除重复及空字符串*/
		for (int i = 0; i < list.size(); i++) {
			Object oldObj=list.get(i);
			
			if("".equals(oldObj)){
				list.remove(i);
				--i;
				continue;
			}
			
			for (int j = i+1; j < list.size(); j++) {
				
				if(j>list.size()){
					break;
				}
				
				Object targetObj=list.get(j);
				
				if(oldObj.equals(targetObj)){
					list.remove(j);
					j--;
				}
			}
		}
		
		return list;
	}
	
	
	

}
